<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon/favicon.ico" />
    <title>动画脚本演示 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="/lib/include-lib.js"
      libpath="/lib/"
      include="jquery,layer,toastr,bootstrap,bootstrap-checkbox,font-awesome,web-icons,jstree,layer,haoutil,nprogress,toastr,admui,mars3d"
    ></script>

    <link href="/css/style.css" rel="stylesheet" />
    <link href="/img/div-graphic/divGraphic.css" rel="stylesheet" />
    <style>
      .viewResult {
        top: 10px;
        left: 10px;
        bottom: auto;
        right:  auto;
        max-width: 420px;
        overflow: auto;
        font-size: 28px;
      }

      .contentUl {
        padding: 0;
        text-align: left;
        font-size: 20px;
        margin-left: 10px;
      }

      .btn i {
        margin-right: 5px;
      }
    </style>
  </head>

  <body class="dark">
    <div id="mars3dContainer" class="mars3d-container"></div>

    <div id="viewReset" class="infoview" style="overflow: auto; top: 10px">
      <div class="btn-group">
        <button id="btn_start" type="button" class="btn btn-primary"><i class="fa fa-play-circle-o"></i>开始</button>
        <button id="btn_pause" type="button" class="btn btn-primary" style="display: none"><i class="fa fa-pause-circle-o"></i>暂停</button>
        <button id="btn_proceed" type="button" class="btn btn-primary" style="display: none"><i class="fa fa-play-circle-o"></i>继续</button>
        <button id="btn_stop" type="button" class="btn btn-primary" style="display: none"><i class="fa fa-stop-circle-o"></i>停止</button>
      </div>

      <ul id="treeOverlays" style="padding: 0"></ul>
      <br />
      总时长: <span id="alltimes">0s</span> &nbsp;&nbsp;
      <br />
      当前:&nbsp;&nbsp;<span id="thisStep">无</span> &nbsp;&nbsp;
      <span id="thistimes"></span>
    </div>

    <script src="/js/common.js"></script>

    <script src="./m35_dhjb.js"></script>
    <script src="./map.js"></script>

    <script type="text/javascript">
      "use script" //开发环境建议开启严格模式

      let isPlay = false
      let isPause = false
      let totalTimes = ""
      let currentWork = ""
      let counter = 0
      let selectedKeys
      let animations = []
      let currentIndex
      let timer
      let interval

      //UI界面相关
      function initUI() {
        initEditorJS(map)

        $("#btn_start").click(function () {
          dataWork.start()
          // play()
        })

        $("#btn_pause").click(function () {
          dataWork.pause()

          pause()
        })

        $("#btn_proceed").click(function () {
          dataWork.proceed()

          $("#btn_start").hide()
          $("#btn_pause").show()
          $("#btn_proceed").hide()
          $("#btn_stop").show()
        })

        $("#btn_stop").click(function () {
          stop()
        })

        let i = 0
        let time = 0
        treeData.forEach((item) => {
          animations = animations.concat(
            item.children.map((it) => {
              time += it.times
              it.index = i
              i++

              return it
            })
          )
        })
        console.log(animations)
        totalTimes = `${Math.floor(time / 60)}分${time % 60}秒`

        map.on(mars3d.EventType.keydown, (event) => {
          switch (event.keyCode) {
            default:
              break
            case "1".charCodeAt(0): //播放、暂停切换
              if (dataWork._isStart) {
                $("#btn_pause").click()
              } else {
                $("#btn_proceed").click()
              }
              break
            case "2".charCodeAt(0): //停止
              $("#btn_stop").click()
              break
            case "3".charCodeAt(0): //绑定和解绑键盘(控制器)控制
              map.keyboardRoam.enabled = !map.keyboardRoam.enabled
              if (map.keyboardRoam.enabled) {
                haoutil.msg("已开启键盘控制")
              } else {
                haoutil.msg("已关闭键盘控制，您可以鼠标进行操作了！")
              }
              break
          }
        })
      }

      const play = () => {
        this.isPlay = true
        this.isPause = false
        start()

        $("#btn_start").hide()
        $("#btn_pause").show()
        $("#btn_proceed").hide()
        $("#btn_stop").show()
      }

      const pause = () => {
        clearTimeout(timer)
        this.currentIndex--
        cancelFlight()
        stopRotatePoint()
        this.isPause = true

        $("#btn_start").hide()
        $("#btn_pause").hide()
        $("#btn_proceed").show()
        $("#btn_stop").show()
      }

      const stop = () => {
        this.isPlay = false
        this.isPause = false
        this.currentIndex = 0
        cancelFlight()
        stopRotatePoint()
        clear()
        clearTimeout(timer)
        clearInterval(interval)
        dataWork.stop()

        $("#btn_start").show()
        $("#btn_pause").hide()
        $("#btn_stop").hide()
        $("#btn_proceed").hide()

        $("#thisStep").html("无")
        $("#thistimes").html("")
        $("#treeOverlays").jstree("deselect_all", true)
      }

      const start = () => {
        if (timer) {
          clearTimeout(timer)
        }
        if (interval) {
          clearInterval(interval)
        }
        if (currentIndex < animations.length) {
          const animate = animations[currentIndex]
          selectedKeys = [animate.key]
          currentWork = `${animate.title}(${animate.times}秒)`
          counter = animate.times
          countOn()
          animate.widget()
          currentIndex++
          timer = setTimeout(() => {
            start()
          }, animate.times * 1000)
        } else {
          stop()
        }
      }

      function countOn() {
        interval = setInterval(() => {
          counter--
          if (counter <= 0) {
            clearInterval(interval)
          }
        }, 1000)
      }

      const treeData = [
        {
          title: "特征点",
          key: "01",
          children: [
            {
              title: "山顶点",
              key: "01-01",
              times: 6,
              widget() {
                workPoint1Sdd()
              }
            },
            {
              title: "鞍部点",
              key: "01-02",
              times: 10,
              widget() {
                workPoint2Abd()
              }
            },
            {
              title: "坡度变换点",
              key: "01-03",
              times: 6,
              widget() {
                workPoint3Pdbhd()
              }
            },
            {
              title: "山脚点",
              key: "01-04",
              times: 6,
              widget() {
                workPoint4Sjd()
              }
            },
            {
              title: "山脚坡度变换点",
              key: "01-05",
              times: 6,
              widget() {
                workPoint5Sjpdbhd()
              }
            },
            {
              title: "倾斜变换点",
              key: "01-06",
              times: 6,
              widget() {
                workPoint6Qxbhd()
              }
            }
          ]
        },
        {
          title: "特征线",
          key: "02",
          children: [
            {
              title: "山脊线",
              key: "02-01",
              times: 6,
              widget() {
                workLine1Sjx()
              }
            },
            {
              title: "山谷线",
              key: "02-02",
              times: 8,
              widget() {
                workLine2Sgx()
              }
            },
            {
              title: "俯瞰",
              key: "02-03",
              times: 5,
              widget() {
                workLine3Fk()
              }
            }
          ]
        },
        {
          title: "绘制过程",
          key: "03",
          children: [
            {
              title: "计算通过点",
              key: "03-01",
              times: 6,
              widget() {
                workDgx1Point()
              }
            },
            {
              title: "等高线绘制",
              key: "03-02",
              times: 6,
              widget() {
                workDgx2Line()
              }
            },
            {
              title: "等高线结果",
              key: "03-03",
              times: 10,
              widget() {
                workDgx3End()
              }
            }
          ]
        }
      ]

      // //数据处理，及自动播放
      let dataWork = {
        alltimes: 0,
        arrNode: [],
        analysisData: function (arr) {
          for (let i = 0; i < arr.length; i++) {
            let item = arr[i]
            // item.state = { disabled: true }; //前端播放时禁用单击

            if (item.widget) {
              item.index = this.arrNode.length //this.getNextId();
              item.id = item.index
              item.times = item.times || 60
              item.text = item.text + "(" + item.times + "秒)"
              item.icon = "fa fa-tag"
              this.arrNode.push(item)
              this.alltimes += item.times
            }

            if (item.children) {
              this.analysisData(item.children)
            }
          }
        },
        steptimes: 0,
        initData: function (data) {
          this.analysisData(data)
          $("#alltimes").html(haoutil.str.formatTime(this.alltimes))

          setInterval(() => {
            if (!this._isStart) {
              return
            }
            this.steptimes++
            $("#thistimes").html(this.steptimes + "秒")
          }, 1000)

          $("#treeOverlays").jstree({
            core: {
              data: data,
              themes: {
                name: "default-dark",
                dots: true,
                icons: true
              }
            }
          })

          $("#treeOverlays").on("changed.jstree", (e, data) => {
            let node = data.node.original
            if (node && node.widget) {
              this.start(node)
            }
          })
        },
        _isStart: false,
        selectdNode: null,
        //开始
        start: function (node) {
          this.stop()

          $("#btn_start").hide()
          $("#btn_pause").show()
          $("#btn_proceed").hide()
          $("#btn_stop").show()

          this._isStart = true
          map.clock.shouldAnimate = true
          dataWork.activateNode(node || this.arrNode[0])
        },
        //暂停
        pause: function () {
          $("#btn_start").hide()
          $("#btn_pause").hide()
          $("#btn_proceed").show()
          $("#btn_stop").show()

          this._isStart = false
          map.cancelFlight()
          JB.stopRotatePoint()

          map.clock.shouldAnimate = false

          if (this.timeIdx && this.timeIdx != -1) {
            clearTimeout(this.timeIdx)
            this.timeIdx = -1
          }
        },
        //   //继续
        proceed: function () {
          $("#btn_start").hide()
          $("#btn_pause").show()
          $("#btn_proceed").hide()
          $("#btn_stop").show()

          map.clock.shouldAnimate = true

          let node = this.selectdNode
          if (node) {
            let that = this

            this.steptimes = 0
            node.widget.disable()
            node.widget.activate()

            this.timeIdx = setTimeout(function () {
              node.widget.disable()
              that.activateNextNode(node.index)
            }, node.times * 1000)
          } else {
            this.start()
          }
          this._isStart = true
        },

        //停止
        stop: function () {
          this.pause()
          $("#btn_start").show()
          $("#btn_pause").hide()
          $("#btn_stop").hide()
          $("#btn_proceed").hide()

          $("#thisStep").html("无")
          $("#thistimes").html("")
          $("#treeOverlays").jstree("deselect_all", true)

          if (this.selectdNode) {
            this.selectdNode.widget.disable()
          }
          dataWork.selectdNode = null
          this._isStart = false

          JB.closePanel()
        },
        activateNode: function (node) {
          this.selectdNode = node
          $("#stopRoate").val("暂停")
          this.steptimes = 0
          $("#thisStep").html(node.text)
          $("#treeOverlays").jstree("deselect_all", true)
          $("#treeOverlays").jstree("select_node", node.id, true)
          node.widget.activate(node)

          // $("#treeOverlays").jstree().close_all(); //收起树

          let that = this
          this.timeIdx = setTimeout(function () {
            node.widget.disable()
            that.activateNextNode(node.index)
          }, node.times * 1000)
        },
        activateNextNode: function (index) {
          index++
          if (index < 0 || index >= this.arrNode.length) {
            this.stop()
            this.selectdNode = null
            return
          }
          let node = this.arrNode[index]
          this.activateNode(node)
        }
      }

      //通用方法 脚本列表
      let JB = {
        showPanel(html) {
          $("#viewResult").remove()
          let innerHTML = `<div id="viewResult" class="infoview viewResult" >
            ${html}
        </div>`
          $("body").append(innerHTML)
        },
        closePanel() {
          $("#viewResult").remove()
        },
        playMP3(src) {
          let mp3 = new Audio(src)
          mp3.play() //播放 mp3这个音频对象
          return mp3
        },
        //绕点飞行
        startRotatePoint(center) {
          if (!this.rotatePoint) {
            this.rotatePoint = new mars3d.thing.RotatePoint({
              direction: false, //方向 true逆时针，false顺时针
              time: 50 //给定飞行一周所需时间(单位 秒)，控制速度
            })
            map.addThing(this.rotatePoint)
          }
          //开启旋转
          this.rotatePoint.start(center)
        },
        stopRotatePoint() {
          if (this.rotatePoint) {
            this.rotatePoint.stop()
          }
        }
      }
    </script>
  </body>
</html>
